package com.tms.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.tms.springboot.base.pojo.PdTransportTrips;
import com.tms.springboot.base.pojo.PdTransportTripsTruckDriver;
import com.tms.springboot.result.JsonResult;
import com.tms.springboot.result.ResultTool;
import com.tms.springboot.service.PdTransportTripsService;
import com.tms.springboot.service.PdTransportTripsTruckDriverService;
import com.tms.springboot.utils.UUId;
import com.tms.springboot.view.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车次信息表 前端控制器
 * </p>
 *
 * @author cjj
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-transport-trips")
public class PdTransportTripsController {


    @Autowired
    private PdTransportTripsService pdTransportTripsService;
    @Autowired
    private PdTransportTripsTruckDriverService transportTripsTruckDriverService;

    @RequestMapping("/queryPdTransportTrips")
    public JsonResult queryPdTransportTrips(@RequestBody PageVo<PdTransportTrips> pageVo) {
        PageHelper.startPage(pageVo.getCurrentPage(), pageVo.getPageSize());
        LambdaQueryWrapper<PdTransportTrips> lqw = new LambdaQueryWrapper<>();
        if (!StringUtil.isEmpty(pageVo.getCondition().getName())) {
            lqw.like(PdTransportTrips::getName, pageVo.getCondition().getName());
        }
        if (!StringUtil.isEmpty(pageVo.getCondition().getTransportLineId())) {
            lqw.eq(PdTransportTrips::getTransportLineId, pageVo.getCondition().getTransportLineId());
        }
        lqw.eq(PdTransportTrips::getStatus, 1);
        List<PdTransportTrips> list = pdTransportTripsService.list(lqw);


        return list != null ? ResultTool.success(new PageInfo<>(list)) : ResultTool.fail();
    }
//车次添加
    @RequestMapping("/addTransportTrips")
    public JsonResult addTransportTrips(@RequestBody PdTransportTrips pdTransportTrips) {
        pdTransportTrips.setId(UUId.nextId());
        pdTransportTrips.setStatus(1);
        boolean save = this.pdTransportTripsService.save(pdTransportTrips);
        return save ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * todo 根据线路id，车次id删除车次表信息, 然后在车辆，车次，司机表中删除绑定
     * @param lineId 线路id
     * @param tripsId 车次id
     * @return
     * writer hui
     */
    @GetMapping("/deleteTripsByTripsIdAndLineId/{lineId}/{tripsId}")
    public JsonResult deleteTripsByTripsIdAndLineId(@PathVariable String lineId, @PathVariable String tripsId) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", tripsId);
        map.put("transport_line_id", lineId);
        boolean flag = this.pdTransportTripsService.removeByMap(map);
        transportTripsTruckDriverService.remove(new LambdaQueryWrapper<PdTransportTripsTruckDriver>().eq(PdTransportTripsTruckDriver::getTransportTripsId, tripsId));
        return flag ? ResultTool.success() : ResultTool.fail();

    }
    /**
     * 车次修改
     * @param pdTransportTrips
     * @return
     */
    @RequestMapping("/updateTransportTrips")
    public JsonResult updateTransportTrips(@RequestBody PdTransportTrips pdTransportTrips){
        return  this.pdTransportTripsService.updateById(pdTransportTrips) ? ResultTool.success():ResultTool.fail();
    }

    /**
     * 车次删除
     * @param pdTransportTrips
     * @return
     */
    @RequestMapping("/deleteTransportTrips")
    public JsonResult deleteTransportTrips(@RequestBody PdTransportTrips pdTransportTrips){
        return this.pdTransportTripsService.removeById(pdTransportTrips) ? ResultTool.success():ResultTool.fail();
    }


}
